﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Manager;
using SdmxMl.Common;

namespace SdmxMl.UiEditors.UC
{
    public partial class UcCategorisation : UserControl
    {
        protected TreeNode treeNode;
        protected CategorisationSourceTarget catSourceTarget;
        protected bool _isFinal;
        
        public UcCategorisation()
        {
            InitializeComponent();
        }
        public virtual bool BindFromNode(TreeNode node, bool isFinal)
        {
            treeNode = node;
            _isFinal = isFinal;
            if (node != null)
            {
                catSourceTarget = node.Tag as CategorisationSourceTarget;

            }
            BindToControls();

            return this.Visible;
        }

        private void BindToControls()
        {
            if (catSourceTarget != null)
            {
                Visible = true;
                bool enabled = _isFinal == false;

                textBoxSourceUrn.Text = catSourceTarget.SourceUrn;
                textBoxTargetUrn.Text = catSourceTarget.TargetUrn;


                // IS Final  treatement
                buttonAddSourceUrl.Enabled = enabled;
                buttonAddTargetUrn.Enabled = enabled;
                buttonNavigateSourceUrl.Enabled = enabled;
                buttonNavigateTargetCategory.Enabled = enabled;
               
            }
            else
                Visible = false;
        }

        private void buttonNavigateUrl_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                    string urn = string.Empty;
                    if (sender == buttonNavigateSourceUrl)
                        urn = UrnParser.UrnSdmx_21_to_20(textBoxSourceUrn.Text);
                    else
                        urn = UrnParser.UrnSdmx_21_to_20(textBoxTargetUrn.Text);

                    // If URN try to get it if not already in memory
                    ArtefactRefTyped art = UrnManager.UrnToArtefactRefTyped(urn);
                    if (art != null)
                    {
                        UrnParser p = new UrnParser(urn);

                        // If already in memory, navigate otherwhile get from regidtry
                        if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, p) == null)
                            NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, p);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void buttonUpdateUrl_Click(object sender, EventArgs e)
        {
            try
            {
                string urn = string.Empty;
                if (Clipboard.ContainsText(TextDataFormat.Text))
                    urn = Clipboard.GetText(TextDataFormat.Text);

                if (urn != null && urn.StartsWith(UrnManager.UrnBase))
                {
                    if (sender == buttonAddTargetUrn)
                    {
                        // Check for category in categoryscheme only
                        UrnParser p = new UrnParser(urn);
                        if (p.IsSupported && p.ArType == SdmxArtefactType.CategorySchemes && string.IsNullOrEmpty(p.SubTypeId) == false)
                        {
                            textBoxTargetUrn.Text = UrnParser.UrnSdmx_20_to_21(urn);
                            catSourceTarget.TargetUrn = textBoxTargetUrn.Text;
                        }
                        else
                            throw new ApplicationException("The Target URN must represent a category of a category scheme !");
                    }
                    else
                    {
                        textBoxSourceUrn.Text = UrnParser.UrnSdmx_20_to_21(urn);
                        catSourceTarget.SourceUrn = textBoxSourceUrn.Text;
                    }
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        
    }
}
